<template>
  <v-data-table :headers="headers" :items="desserts" sort-by="calories" class="elevation-1">
    <template v-slot:top>
      <v-toolbar flat>
        <v-toolbar-title>外部产品与核算产品映射</v-toolbar-title>
        <v-divider class="mx-4" inset vertical></v-divider>
        <v-spacer></v-spacer>
        <v-dialog v-model="dialog" max-width="800px">
          <template v-slot:activator="{ on }">
            <v-btn color="primary" dark class="mb-2" v-on="on">新增</v-btn>
          </template>
          <v-card>
            <v-card-title>
              <span class="headline">{{ formTitle }}</span>
            </v-card-title>

            <v-card-text>
              <v-container>
                <v-row>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.extprdno" label="外部产品编码"></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.extprdnm" label="外部产品名称"></v-text-field>
                  </v-col>
                  <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accprdno" label="核算产品编号"></v-text-field>
                  </v-col>
                   <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accprdnm" label="核算产品名称"></v-text-field>
                  </v-col> -->

                      <v-col cols="12" sm="6" md="4">
                  <v-select :items="accprdnoItems" 
                  label="核算产品" 
                  v-model="editedItem.accprdno"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>

                  <v-col cols="12" sm="6" md="4">
                  <v-select :items="rcdstsItems" 
                  label="记录状态" 
                  v-model="editedItem.rcdsts"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>
                </v-row>
              </v-container>
            </v-card-text>

            <v-card-actions>
              <v-spacer></v-spacer>
              <v-btn color="blue darken-1" text @click="close">取消</v-btn>
              <v-btn color="blue darken-1" text @click="save">保存</v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </v-toolbar>
    </template>
    <template v-slot:item.actions="{ item }">
      <v-icon small class="mr-2" @click="editItem(item)">mdi-pencil</v-icon>
      <v-icon small @click="deleteItem(item)">mdi-delete</v-icon>
    </template>

    <template v-slot:item.rcdsts="{ item }">
    
      <span v-for="s in rcdstsItems" key="s.id" v-if="s.attributes==item.rcdsts">{{s.name}}</span>
    </template>

     <template v-slot:item.accprdnm="{ item }">
    
      <span v-for="s in accprdnoItems" key="s.id" v-if="s.attributes==item.accprdno">{{s.name}}</span>
    </template>


    <template v-slot:no-data>
      <v-btn color="primary" @click="initialize">Reset</v-btn>
    </template>
  </v-data-table>
</template>

<script>
export default {
  data: () => ({
    rcdstsItems: [],
    accprdnoItems:[],
    dialog: false,
    headers: [
      {
        text: "外部产品编码",
        align: "start",
        value: "extprdno"
      },
      { text: "外部产品名称", value: "extprdnm" },
      { text: "核算产品编码", value: "accprdno" },
      { text: "核算产品名称", value: "accprdnm" },
      { text: "记录状态", value: "rcdsts" },
      { text: "操作", value: "actions", sortable: false }
    ],
    desserts: [],
    editedIndex: -1,
    editedItem: {
      extprdno: "",
      extprdnm: "",
      accprdno:"",
      accprdnm: "",
      rcdsts:"1",
    },
    defaultItem: {
      extprdno: "",
      extprdnm: "",
      accprdno:"",
      accprdnm: "",
      rcdsts:"1",
    }
  }),

  computed: {
    formTitle() {
      return this.editedIndex === -1 ? "新增" : "修改";
    }
  },

  watch: {
    dialog(val) {
      val || this.close();
    }
  },

  created() {
    this.initialize();
    this.getDict()
  },

  methods: {
    initialize() {
     this.axios
        .get("/acfd05/list")
        .then(response => {
          this.desserts = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
    },
    getDict(){

       this.axios
        .get("/dict/topName/记录状态")
        .then(response => {
          this.rcdstsItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
         this.axios
        .get("/dict/topName/核算产品")
        .then(response => {
          this.accprdnoItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
       
    },
    editItem(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },

    deleteItem(item) {
      const index = this.desserts.indexOf(item);
      confirm("确定要删除此条记录吗?") &&
        this.axios.delete('/acfd05/'+item.id)
          .then(res=>{
            this.desserts.splice(index, 1);
          })
        
    },

    close() {
      this.dialog = false;
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      });
    },

    save() {
       this.axios.post("/acfd05", this.$qs.stringify(this.editedItem)).then(res => {
          this.initialize();
          this.close();
      });
      
    }
  }
};
</script>